package view;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
import model.*;

public class CurrentPoiInfoViewDecorator extends ViewDecorator implements Observer {
    
    private Map map;
    private PointOfInterestWatcher poiWatcher;
    private AbstractDistanceCalculator distanceCalculator;
    private GpsReceiver gpsReceiver;
    
    public CurrentPoiInfoViewDecorator(JComponent component,
            Map map, PointOfInterestWatcher poiWatcher,
            AbstractDistanceCalculator distanceCalculator,
            GpsReceiver gpsReceiver) {
        
        super(component);
        this.map = map;
        this.poiWatcher = poiWatcher;
        this.distanceCalculator = distanceCalculator;
        this.gpsReceiver = gpsReceiver;
        
        this.poiWatcher.attach(this);
        this.gpsReceiver.attach(this);
        
        update();
    }
    
    @Override
    public void update() {
        this.repaint();
    }
    
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2d = (Graphics2D)g;
        
        //
        RouteWaypoint waypoint = poiWatcher.getNextWaypoint();
        String poiName = waypoint.getPointOfInterest().getName();
        
        // Bereken afstand
        GpsCoordinate waypointCoord = waypoint.getGpsCoordinate();
        GpsCoordinate currentCoord = gpsReceiver.getGpsCoordinate();
        double distance = Math.round(distanceCalculator.getDistance(waypointCoord, currentCoord));
        
        String unit = distanceCalculator.getUnit();
        
        // Tekenen
        g2d.setColor(new Color(255, 255, 255, 200));
        g2d.fillRect(0, 0, 1000, 50);
        
        g2d.setColor(Color.BLACK);
        Font font = new Font("Dialog", Font.BOLD, 20);
        g2d.setFont(font);
        g2d.drawString(poiName, 8, 24);
        
        g2d.setColor(Color.BLACK);
        font = new Font("Dialog", Font.PLAIN, 12);
        g2d.setFont(font);
        g2d.drawString(Double.toString(distance) + " " + unit, 8, 40);
    }
}
